package cn.lbz.Food.Edit;

import cn.lbz.Food.Init.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.io.Serial;
import java.sql.SQLException;

public class UpdateFoodServlet extends HttpServlet{
    @Serial
    private static final long serialVersionUID = 5370646573277429191L;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id =request.getParameter("id");
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String located = request.getParameter("located");
        String type = request.getParameter("type");
        String reason = request.getParameter("reason");
        String sql = String.format("UPDATE `share_food` SET `name`= \"%s\", `located`=\"%s\", `price`=%s, `type`=\"%s\", `reason`=\"%s\", WHERE `id`=%s", name, located, price, type, reason, id);
        System.out.println(sql);
        try {
            DBUtils.update(sql);
            response.sendRedirect("./editfood");
            JOptionPane.showMessageDialog(null, "编辑成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
